
class MyBlockingQueue{
    private String[] data=null;
    private volatile int head =0;
    private volatile int tail =0;
    private volatile int size =0;

    public MyBlockingQueue(int capacity){
        data = new String[capacity];
    }

    public void put(String s) throws InterruptedException {
        synchronized (this){
            //下面有大量的修改操作,加上锁保证线程安全
            //可以单独定义一个锁对象,也可以使用this表示当前对象
            while(size == data.length){
                //队列满了
//                return;
                this.wait();
            }
            data[tail]=s;
            tail++;
            if(tail >= data.length){
                tail=0;//如果tail移到了末尾,直接让tail到0位置,构成循环队列
            }
            size++;

            this.notify();
        }
    }

    public String take() throws InterruptedException {
        String ret= "";
        synchronized (this){
            while(size == 0){
//                return null;
                this.wait();
            }
             ret = data[head];

            head++;
            if(head>=data.length){
                head = 0;
            }
            size--;

            this.notify();
        }
        return ret;
    }
}


public class Demo28 {
}
